package com.ruoyi.carApi.callBack;

import com.ruoyi.carApi.api.IPlateCallback;
import com.ruoyi.carApi.click.ICarCallBack;
import com.ruoyi.carApi.entity.CarIdentification;
import com.sun.jna.Pointer;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2021/6/22 9:21
 */
public class PlateCallback implements IPlateCallback {

    public static ICarCallBack iCarCallBack;

    /**
     * 车辆类型
     */
    private static Map<Integer,String> carTypeMap = new HashMap<>();
    static {
        carTypeMap.put(0,"未知");
        carTypeMap.put(1,"轿车");
        carTypeMap.put(2,"面包车");
        carTypeMap.put(3,"大型客车");
        carTypeMap.put(4,"中型客车");
        carTypeMap.put(5,"皮卡");
        carTypeMap.put(6,"非机动车");
        carTypeMap.put(7,"SUV");
        carTypeMap.put(8,"MPV");
        carTypeMap.put(9,"微型货车");
        carTypeMap.put(10,"轻型货车");
        carTypeMap.put(11,"中型货车");
        carTypeMap.put(12,"重型货车");
    }

    /**
     * 常驻
     */
    private static Map<Integer,String> carTypePerMap = new HashMap<>();
    static {

        carTypePerMap.put(3,"");
        carTypePerMap.put(4,"");
        carTypePerMap.put(5,"");
        carTypePerMap.put(12,"");
        carTypePerMap.put(13,"");
        carTypePerMap.put(14,"");
    }
    /**
     * 临时
     */
    private static Map<Integer,String> carTypeTemMap = new HashMap<>();
    static {
        carTypeTemMap.put(0,"");
        carTypeTemMap.put(1,"");
        carTypeTemMap.put(2,"");
        carTypeTemMap.put(9,"");
        carTypeTemMap.put(10,"");
        carTypeTemMap.put(11,"");
    }

    @Override
    public void ICE_IPCSDK_Plate(int param, String strIP, String strNumber, String strColor, Pointer pPicData, int nLen, Pointer pCloseupData, int nCloseupLen, int nPlatePosLeft, int nPlatePosTop, int nPlatePosRight, int nPlatePosBottom, float fPlateConfidence, int nVehicleColor, int nPlateType, int nVehicleDir, int nAlarmType, int nReserved1, int nReserved2, int nReserved3, int nReserved4) {
        //回调函数未设置，跳过
        if(iCarCallBack == null){
            return;
        }
        CarIdentification carIdentification = new CarIdentification();
        carIdentification.setIp(strIP);
        carIdentification.setColor(strColor);
        carIdentification.setPlateNumber(strNumber);
        carIdentification.setCarTypeName(carTypeMap.get(nPlatePosTop));
        // 报警类型  0，1，2，9，10，11 ，临时车辆 3,4,5,12,13,14, 常驻
        String s = carTypePerMap.get(nAlarmType);
        if(s != null){
            //常驻车
            carIdentification.setCarType(0);
        }
        String s1 = carTypeTemMap.get(nAlarmType);
        if(s1 != null){
            //临时车
            carIdentification.setCarType(1);
        }
        if(nLen > 0){
            byte[] dst = new byte[nLen];
            pPicData.read(0, dst, 0, nLen);
            //存全景图
            carIdentification.setPanoramaImageData(dst);
        }

        if(nCloseupLen > 0){
            byte[] dst = new byte[nCloseupLen];
            pCloseupData.read(0, dst, 0, nCloseupLen);
            //存车牌图
            carIdentification.setPlateNumberImageData(dst);
        }

        iCarCallBack.invoke(carIdentification);
    }

    //保存图片
    public static void savePic(String strIP, String strNumber, byte[] data, int type, long lTime)
    {
        String strPath = "D:/抓拍_java/";
        Date now = null;

        if(lTime == 0){
            now = new Date();
        }else{
            now = new Date(lTime);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dateNow = dateFormat.format(now);
        String dateNow2 = dateFormat2.format(now);

        strPath += strIP + "/" + dateNow;
        File file = new File(strPath);
        if(!file.exists())
        {
            file.mkdirs();
        }
        String filename = strPath + "/" + dateNow2 + "_" + strNumber;
        filename = filename.trim();
        //车牌图
        if(1 == type)
        {
            filename += "_plate";
        }
        //手动抓拍图
        else if(2 == type)
        {
            filename += "_Capture";
        }
        else if(3 == type)
        {
            //软触发图
            filename += "_trigger";
        }
        filename += ".jpg";


        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(filename));
            out.write(data);
            out.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

//		FileImageOutputStream imgOut;
//		try {
//			imgOut = new FileImageOutputStream(new File(filename));
//			imgOut.write(data, off, len);
//			imgOut.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }
}
